<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="110px">
        <el-form-item label="订单号" prop="pay_sn">
          <el-input
            v-model="queryParams.pay_sn"
            placeholder="请输入订单编号"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="项目名称" prop="pro_name">
          <el-input
            v-model="queryParams.pro_name"
            placeholder="请输入项目名称"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="经理人名称" prop="jlr_name">
          <el-input
            v-model="queryParams.jlr_name"
            placeholder="请输入经理人名称"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="经理人手机号" prop="jlr_phone">
          <el-input
            v-model="queryParams.jlr_phone"
            placeholder="请输入经理人手机号"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="建企名称" prop="company_name">
          <el-input
            v-model="queryParams.company_name"
            placeholder="请输入建企名称"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="是否结清" prop="is_price_end">
          <el-select v-model="queryParams.is_price_end" placeholder="支付状态" clearable>
            <el-option
              label="是"
              :value="1"
            />
            <el-option
              label="否"
              :value="2"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="日期">
          <el-date-picker
            v-model="dateRange"
            style="width: 240px"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
  
      <el-row :gutter="10" class="mb8">

        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['system:config:export']"
          >导出</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
  
      <el-table v-loading="loading" :data="configList" @selection-change="handleSelectionChange">
        <el-table-column label="订单编号" align="center" prop="pay_sn" :show-overflow-tooltip="true" />
        <el-table-column label="项目名称" align="center" prop="pro_name" :show-overflow-tooltip="true" >
          <template slot-scope="scope">
            <el-popover
              @show="showContentXm(scope.row)"
              placement="top-start"
              title="项目信息"
              width="400"
              trigger="click">
              <div v-if="scope.row.xmDeatil">
                <el-row>
                  <el-col :span="24">{{ scope.row.xmDeatil.company_name }}</el-col>
                  <el-col :span="24">营业执照： <el-image
                    style="width: 100px; height: 100px"
                    :src="scope.row.xmDeatil.business_licence_number_electronic"
                    fit="contain"></el-image></el-col>
                  <el-col :span="24">开票信息</el-col>
                  <el-col :span="24">税号：{{ scope.row.xmDeatil.taxNumber }}</el-col>
                  <el-col :span="24">电话：{{ scope.row.xmDeatil.legal_mobile }}</el-col>
                  <el-col :span="24">开户行：{{ scope.row.xmDeatil.bank_name }}</el-col>
                  <el-col :span="24">银行卡号：{{ scope.row.xmDeatil.bank_acct_no }}</el-col>
                </el-row>
              </div>
              <div slot="reference"> {{ scope.row.pro_name }}</div>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column label="经理人名称" width="100" align="center" prop="jlr_name" :show-overflow-tooltip="true" >
          <template slot-scope="scope">
            <el-popover
              @show="showContent(scope.row)"
              placement="top-start"
              title="经理人信息"
              width="400"
              trigger="click">
              <div v-if="scope.row.jlrDeatil">
                <el-row>
                  <el-col :span="24">姓名：{{ scope.row.jlrDeatil.title }}</el-col>
                  <el-col :span="24">手机号：{{ scope.row.jlrDeatil.tel }}</el-col>
                  <el-col :span="24">银行卡：{{ scope.row.jlrDeatil.bank_card_number }}</el-col>
                  <el-col :span="24">身份证号：{{ scope.row.jlrDeatil.id_num }}</el-col>
                  <el-col :span="24">本单分成：{{ scope.row.jlrDeatil.bid_fee }}</el-col>
                  <el-col :span="24">累计金额：{{ scope.row.jlrDeatil.total_fee }}</el-col>
                  <el-col :span="24">
                    <el-image
                    style="width: 100px; height: 100px"
                    :src="scope.row.jlrDeatil.id_card_a"
                    fit="contain"></el-image>
                    <el-image
                    style="width: 100px; height: 100px"
                    :src="scope.row.jlrDeatil.id_card_b"
                    fit="contain"></el-image>
                  </el-col>
                </el-row>
              </div>
              <div slot="reference"> {{ scope.row.jlr_name }}</div>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column label="经理人手机号" width="120" align="center" prop="jlr_phone" :show-overflow-tooltip="true" />
        <el-table-column label="建企名称" align="center" prop="company_name" :show-overflow-tooltip="true" />
        <el-table-column label="支付时间" align="center" prop="s_time" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.payment_time) }}</span>
          </template>
        </el-table-column>
        <!-- <el-table-column label="结束时间" align="center" prop="e_time" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.e_time) }}</span>
          </template>
        </el-table-column> -->
        <el-table-column label="是否结清" align="center" prop="is_price_end">
          <template slot-scope="scope">
            <el-switch
                  v-model="scope.row.is_price_end"
                  :active-value="1"
                  :inactive-value="2"
                  @change="handleStatusChange(scope.row)"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="是否开发票" align="center" prop="bid_invoice_status">
          <template slot-scope="scope">
             {{ scope.row.bid_invoice_status === 0 ? '未开' : scope.row.bid_invoice_status === 1 ? '开票中' : scope.row.bid_invoice_status === 1 ? '已开票': '作废' }}
          </template>
        </el-table-column>
      </el-table>
  
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.page"
        :limit.sync="queryParams.page_size"
        @pagination="getList"
      />
  
      <!-- 添加或修改参数配置对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="项目名称" prop="pro_name">
            <el-input v-model="form.pro_name" placeholder="请输入项目名称" />
          </el-form-item>
          <el-form-item label="经理人姓名" prop="jlr_name">
            <el-input v-model="form.jlr_name" placeholder="请输入经理人姓名" />
          </el-form-item>
          <el-form-item label="经理人手机号" prop="jlr_phone">
            <el-input v-model="form.jlr_phone" placeholder="请输入经理人手机号" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </template>
  
  <script>
  import { plat_list,change_is_price_end,get_jlr_info_by_tel,get_company_info_by_eid } from "@/api/finance/index";
  
  export default {
    name: "Config",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 参数表格数据
        configList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 日期范围
        dateRange: [],
        // 查询参数
        queryParams: {
          page: 1,
          page_size: 10,
          configName: undefined,
          configKey: undefined,
          configType: undefined
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          configName: [
            { required: true, message: "参数名称不能为空", trigger: "blur" }
          ],
          configKey: [
            { required: true, message: "参数键名不能为空", trigger: "blur" }
          ],
          configValue: [
            { required: true, message: "参数键值不能为空", trigger: "blur" }
          ]
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询参数列表 */
      getList() {
        this.loading = true;
        plat_list(this.addDateRange(this.queryParams, this.dateRange,'e_time')).then(response => {
            this.configList = response.data.list;
            this.total = response.data.totalCount;
            this.loading = false;
          }
        );
      },
      showContent(row) {
        //请求经理人详情
        if(!row.jlrDeatil){
            get_jlr_info_by_tel({'jlr_phone':row.jlr_phone,'bid':row.bid,'s_time':row.s_time,'e_time':row.e_time}).then(res=>{
            this.$nextTick(()=>{
              this.$set(row,'jlrDeatil',res.data)
            })  
          })
        }
      },
      //请求项目详情信息
      showContentXm(row){
        if(!row.xmDeatil){
          get_company_info_by_eid({'eid':row.eid}).then(res=>{
            this.$nextTick(()=>{
              this.$set(row,'xmDeatil',res.data)
            })  
          })
        }
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          configId: undefined,
          configName: undefined,
          configKey: undefined,
          configValue: undefined,
          configType: "Y",
          remark: undefined
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.page = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.dateRange = [];
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加参数";
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.configId)
        this.single = selection.length!=1
        this.multiple = !selection.length
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const configId = row.configId || this.ids
        getConfig(configId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改参数";
        });
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.configId != undefined) {
              updateConfig(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addConfig(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const configIds = row.configId || this.ids;
        this.$modal.confirm('是否确认删除参数编号为"' + configIds + '"的数据项？').then(function() {
            return delConfig(configIds);
          }).then(() => {
            this.getList();
            this.$modal.msgSuccess("删除成功");
          }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('admin/finance/plat_excel_export', {
          ...this.queryParams
        }, `config_${new Date().getTime()}.xlsx`)
      },
      /** 刷新缓存按钮操作 */
      handleRefreshCache() {
        refreshCache().then(() => {
          this.$modal.msgSuccess("刷新成功");
        });
      },
      handleStatusChange(row) {
        console.log(row)
      let text = row.is_price_end === "1" ? "结清" : "未结清";
      this.$modal.confirm('确认要"' + text + '""' + row.pro_name + '"项目？').then(function() {
        return change_is_price_end({'bid':row.bid,'is_price_end': row.is_price_end});
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function() {
        row.is_price_end = row.is_price_end === "1" ? "2" : "1";
      });
    },
    }
  };
  </script>
  